<?php

//require_once ('dompdf/dompdf_config.inc.php');
class DompdfController extends Model3_Controller {

    public function init() {
        /* No incluir ningun archivo de css ya que hace que el pdf deje de funcionar */
    }

    public function indexAction() {
        $dbs = Model3_Registry::getInstance()->get('databases');
        $em = $dbs['DefaultDb'];

        $this->view->setUseTemplate(false);
        $startDate = $this->getRequest()->getParam('dateIni');
        $endDate = $this->getRequest()->getParam('dateFin');
        $ruta = explode('_',$this->getRequest()->getParam('Graficas'));
        /* @var $em Doctrine\ORM\EntityManager */
        $invoiceAdapter = $em->getRepository('DefaultDb_Entities_Facturas');
        $invoice = $invoiceAdapter->findBetween($startDate, $endDate);


        $today = new DateTime();
        $this->view->invoice = $invoice;
        $this->view->startDate = $startDate;
        $this->view->endDate = $endDate;
        $this->view->today = $today->format('Y-m-d');
        $this->view->urlGraphics = 'images/Graficas/'.$ruta[0].'.png';
        $this->view->azul ='images/Graficas/barraAzul.png';
        $this->view->verde ='images/Graficas/barraVerde.png';
        $this->view->rojo ='images/Graficas/barraRojo.png';
    }

    public function invoicePdfAction() {
        $this->view->setUseTemplate(false);
        $dbs = Model3_Registry::getInstance()->get('databases');
        $em = $dbs['DefaultDb'];
        $idCliente = $this->getRequest()->getParam('client');
        /* @var $em Doctrine\ORM\EntityManager */
        $eventAdapter = $em->getRepository('DefaultDb_Entities_Eventos');
        $invoiceAdapter = $em->getRepository('DefaultDb_Entities_Facturas');

        $today = new DateTime();
        $event = $eventAdapter->findBy(array('cliente' => $idCliente ));
        $client = $em->find('DefaultDb_Entities_Usuarios', $idCliente);
        if ($event) 
        {
            $invoice = $invoiceAdapter->findBy(array('evento' => $event[0]->getId()));
            if ($invoice) 
            {
                $this->view->event = $event[0];
                $this->view->invoice = $invoice[0];
                $this->view->today = $today->format('d-m-Y');
            }
        }
        else
            $this->view->error = true;

        $this->view->cliente = $client;

    }

    public function invoiceDatePdfAction() {
        $this->view->setUseTemplate(false);
        $startDate = $this->getRequest()->getParam('startDate');
        $endDate = $this->getRequest()->getParam('endDate');
        /* @var $em Doctrine\ORM\EntityManager */
        $dbs = Model3_Registry::getInstance()->get('databases');
        $em = $dbs['DefaultDb'];

        $invoiceAdapter = $em->getRepository('DefaultDb_Entities_Facturas');
        $today = new DateTime();

        $invoices = $invoiceAdapter->findBetween($startDate, $endDate);
        $this->view->invoices = $invoices;
        $this->view->fechaIni = $startDate;
        $this->view->fechaFin = $endDate;
        $this->view->today = $today->format('d-m-Y');
    }

    public function utilityPdfAction() {
        $this->view->setUseTemplate(false);
        $dbs = Model3_Registry::getInstance()->get('databases');
        $em = $dbs['DefaultDb'];
        $idEvent = $this->getRequest()->getParam('event');
        $ruta = explode('_',$this->getRequest()->getParam('Graficas'));
        /* @var $em Doctrine\ORM\EntityManager */
        $detallesXEvento = $em->getRepository('DefaultDb_Entities_DetalleXPaquete');
        $event = $em->find('DefaultDb_Entities_Eventos', $idEvent);
        $client = $event->getCliente();
        $detallesDelPaquete = $detallesXEvento->findBy(array('paquete' => $event->getPaquete()->getId())); 
        
         
        $this->view->evento = $event;
        $this->view->cliente = $client;
        $this->view->detallesXPaquete = $detallesDelPaquete;
        $this->view->urlGraphics = 'images/Graficas/'.$ruta[0].'.png';
    }

    public function reservationPdf()
    {
        $this->view->setUseTemplate(false);
        /*@var $em Doctrine\ORM\EntityManager */
        $dbs = Model3_Registry::getInstance()->get('databases');
        $em = $dbs['DefaultDb'];

        $eventAdapter = $em->getRepository('DefaultDb_Entities_Eventos');
        $this->getRequest()->getParam('fechaIni');
        $fechaIni = $post['fechaIni'];
        $fechaFin = $post['fechaFin'];
        $eventos = $eventAdapter->getEventsBetweenDate($fechaIni,$fechaFin);

        $this->view->fechaIni = $fechaIni;
        $this->view->fechaFin = $fechaFin;
        $this->view->eventos = $eventos;
    }
    
    public function demandaEventosAction()
    {
        $this->view->setUseTemplate(false);
        /*@var $em Doctrine\ORM\EntityManager */
        $dbs = Model3_Registry::getInstance()->get('databases');
        $em = $dbs['DefaultDb'];

        $eventAdapter = $em->getRepository('DefaultDb_Entities_Eventos');
        $anio = $this->getRequest()->getParam('year');
        $ruta = explode('_',$this->getRequest()->getParam('Graficas'));

        $today = new DateTime();
        $fechaIni = new DateTime();
        $fechaFin = new DateTime();

        $fechaIni = $fechaIni->setDate($anio-1, 12, 31);
        $fechaFin = $fechaFin->setDate($anio+1, 1, 1);
        $fechaIni = $fechaIni->format('Y-m-d');
        $fechaFin = $fechaFin->format('Y-m-d');
        $eventosMañana = $eventAdapter->getEventMorning($fechaIni,$fechaFin);
        $eventosTarde = $eventAdapter->getEventafternoon($fechaIni,$fechaFin);
        $eventosNoche = $eventAdapter->getEventNight($fechaIni,$fechaFin);
        $totalEventos = count($eventosMañana) + count($eventosTarde) + count($eventosNoche);

        $this->view->urlGraphics = 'images/Graficas/'.$ruta[0].'.png';
        $this->view->noche = 'images/Graficas/noche.png';
        $this->view->tarde = 'images/Graficas/tarde.png';
        $this->view->manana = 'images/Graficas/manana.png';
        $this->view->today = $today->format('Y-m-d');
        $this->view->fechaIni = $fechaIni;
        $this->view->fechaFin = $fechaFin;
        $this->view->numEventos = $totalEventos;
        $this->view->eventosMañana = count($eventosMañana);
        $this->view->eventosTarde = count($eventosTarde);
        $this->view->eventosNoche = count($eventosNoche);
    }    
}